package com.landiao.ctrler.management;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.landiao.common.factory.ResultMsgFactory;
import com.landiao.common.pojo.Page;
import com.landiao.common.pojo.Querier;
import com.landiao.common.pojo.ResultMsg;
import com.landiao.common.utils.CodecUtils;
import com.landiao.common.utils.DateUtil;
import com.landiao.ctrler.BaseCRUDCtrler;
import com.landiao.entities.DyeRecord;
import com.landiao.entities.HairdoImg;
import com.landiao.entities.HairdoRecord;
import com.landiao.entities.IdentifyCode;
import com.landiao.entities.MdseBuyRecord;
import com.landiao.entities.NoteDict;
import com.landiao.entities.PayFlow;
import com.landiao.entities.PermRecord;
import com.landiao.entities.Vip;
import com.landiao.service.DyeRecordService;
import com.landiao.service.HairdoImgService;
import com.landiao.service.HairdoRecordService;
import com.landiao.service.IdentifyCodeService;
import com.landiao.service.MdseBuyRecordService;
import com.landiao.service.NoteDictService;
import com.landiao.service.PayFlowService;
import com.landiao.service.PermRecordService;
import com.landiao.service.VipService;

@Controller
@RequestMapping("/management/vip")
public class VipCtrler extends BaseCRUDCtrler<Vip, String> {
	@Autowired
	private DyeRecordService dyeRecordService;
	@Autowired
	private PermRecordService permRecordService;
	@Autowired
	private HairdoRecordService hairdoRecordService;
	@Autowired
	private MdseBuyRecordService mdseBuyRecordService;
	@Autowired
	private HairdoImgService hairdoImgService;
	@Autowired
	private PayFlowService payFlowService;
	@Autowired
	private NoteDictService noteDictService;
	@Autowired
	private IdentifyCodeService identifyCodeService;

	private VipService getVipService() {
		return (VipService) baseService;
	}

	@Override
	public String save(Vip t, RedirectAttributes redirectAttr) {
		// TODO Auto-generated method stub
		t.setCompId(this.getClientUser().getUser().getCompId());
		t.setVipId(CodecUtils.getUUID());
		if (t.getPhoto() != null && !t.getPhoto().equals("")) {
			t.setPhoto(this.getphoto(t.getPhoto()));
		}
		t.setStatedate(new Date());
		baseService.save(t);
		redirectAttr.addFlashAttribute("resultMsg",
				ResultMsgFactory.success("添加成功"));
		return redirectToUrl(viewName("index"));
	}

	@Override
	public String update(Vip t, RedirectAttributes redirectAttr) {
		// TODO Auto-generated method stub
		if (t.getPhoto() != null && !t.getPhoto().equals("")) {
			t.setPhoto(this.getphoto(t.getPhoto()));
		}
		return super.update(t, redirectAttr);
	}

	@RequestMapping("/balanceIndex")
	public String balanceIndex() {
		return "management/vip/balanceIndex";
	}

	@RequestMapping("/buyIndex")
	public String buyIndex() {
		return "management/vip/buyIndex";
	}
	@RequestMapping("/ruleIndex")
	public String ruleIndex() {
		return "management/vip/ruleIndex";
	}
	@RequestMapping(value = "rulesave")
	public String rulesave(NoteDict t, RedirectAttributes redirectAttr) {
		// TODO Auto-generated method stub
		t.setCompId(this.getClientUser().getUser().getCompId());
		t.setTabName("SYSTEM");
		t.setColName("RULE");
		t.setVal3Desc(CodecUtils.getUUID());
		noteDictService.save(t);
		redirectAttr.addFlashAttribute("resultMsg",
				ResultMsgFactory.success("添加成功"));
		return redirectToUrl(viewName("ruleIndex"));
	}
	@RequestMapping(value = "ruleupdate")
	public String ruleupdate(NoteDict t,RedirectAttributes redirectAttr) {
		try{
			noteDictService.update(t);
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.success("修改成功"));
		}catch(Exception e){
			e.printStackTrace();
			log.error("修改失败：{}",e.getMessage());
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("修改失败"));
		}
		return redirectToUrl("management/vip/ruleIndex");
	}
	@RequestMapping(value = "ruleadd", method = RequestMethod.GET)
	public String ruleadd(Model model) {
		return viewName("ruleadd");
	}
	@ResponseBody
	@RequestMapping(value = "ruledelete/{id}", method = RequestMethod.POST)
	public ResultMsg ruledelete(@PathVariable String id) {
		try{
			noteDictService.delete(id);
			return ResultMsgFactory.success("删除成功");
		}catch(Exception e){
			e.printStackTrace();
			log.error("删除失败：{}",e.getMessage());
			return ResultMsgFactory.success("删除失败");
		}
	}
	@RequestMapping(value = "ruleedit/{id}", method = RequestMethod.GET)
	public String ruleedit(Model model, @PathVariable String id) {
		NoteDict t = new NoteDict();
		t.setCompId(this.getClientUser().getUser().getCompId());
		t.setTabName("SYSTEM");
		t.setColName("RULE");
		t.setVal3Desc(id);
		NoteDict result = noteDictService.getByval3Desc(t);
		model.addAttribute("t", result);
		return viewName("ruleedit");
	}
	
	@RequestMapping(value = "/balanceEdit/{vipId}", method = RequestMethod.GET)
	public ModelAndView balanceEdit(@PathVariable String vipId) {
		System.out.println("vipId=" + vipId);
		Vip vip = baseService.getById(vipId);
		ModelAndView model = new ModelAndView("management/vip/balanceEdit");
		model.addObject("vip", vip);

		return model;
	}

	@RequestMapping(value = "balanceUpdate")
	public String balanceUpdate(Vip vip, RedirectAttributes redirectAttr) {
		payFlowService.updateVipBalance(vip.getCompId(), vip.getVipId(), vip.getBalance());
		redirectAttr.addFlashAttribute("resultMsg",
				ResultMsgFactory.success("修改成功"));
		return redirectToUrl("management/vip/balanceIndex");
	}

	@ResponseBody
	@RequestMapping("dyepage")
	public Page<DyeRecord> dyepage(DyeRecord dyeRecord, Querier querier) {
		Page<DyeRecord> page = dyeRecordService.queryByPage(querier, dyeRecord);
		return page;
	}

	@ResponseBody
	@RequestMapping("permpage")
	public Page<PermRecord> permpage(PermRecord permRecord, Querier querier) {
		Page<PermRecord> page = permRecordService.queryByPage(querier,
				permRecord);
		return page;
	}

	@ResponseBody
	@RequestMapping("hairdopage")
	public Page<HairdoRecord> hairdopage(HairdoRecord hairdoRecord,
			Querier querier) {
		Page<HairdoRecord> page = hairdoRecordService.queryByPage(querier,
				hairdoRecord);
		return page;
	}

	@ResponseBody
	@RequestMapping("mdsebuypage")
	public Page<MdseBuyRecord> mdsebuypage(MdseBuyRecord mdseBuyRecord,
			Querier querier) {
		Page<MdseBuyRecord> page = mdseBuyRecordService.queryByPage(querier,
				mdseBuyRecord);
		return page;
	}
	@ResponseBody
	@RequestMapping("rulepage")
	public Page<NoteDict> rulepage(NoteDict t,
			Querier querier) {
		t.setColName("RULE");
		t.setTabName("SYSTEM");
		t.setCompId(this.getClientUser().getUser().getCompId());
		Page<NoteDict> page = noteDictService.queryByPage(querier, t);
		return page;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "setstar/{id}/{isStar}", method = RequestMethod.POST)
	public ResultMsg delete(@PathVariable String id, @PathVariable String isStar) {
		try {
			Vip vip = new Vip();
			vip.setVipId(id);
			vip.setIsStar(isStar);
			getVipService().updateByPrimaryKeySelective(vip);
			return ResultMsgFactory.success("标星成功");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("标星失败：{}", e.getMessage());
			return ResultMsgFactory.success("标星失败");
		}
	}

	/**
	 * 验证账号是否存在
	 * 
	 */
	@RequestMapping("isExist")
	@ResponseBody
	public boolean isExist(String vipNo, String id) {
		Vip user = this.getVipService().getByVipNo(vipNo);
		if (user == null) {
			return true;
		} else {
			if (user.getVipId().equals(id)) {
				return true;
			} else {
				return false;
			}
		}
	}

	@RequestMapping(value = "dyedetail/{id}", method = RequestMethod.GET)
	public String dyedetail(Model model, @PathVariable String id) {
		DyeRecord t = dyeRecordService.getById(id);
		model.addAttribute("t", t);
		return viewName("dyedetail");
	}

	@RequestMapping(value = "permdetail/{id}", method = RequestMethod.GET)
	public String permdetail(Model model, @PathVariable String id) {
		PermRecord t = permRecordService.getById(id);
		model.addAttribute("t", t);
		return viewName("permdetail");
	}

	@RequestMapping(value = "hairdodetail/{id}", method = RequestMethod.GET)
	public String dyeedit(Model model, @PathVariable String id) {
		HairdoRecord t = hairdoRecordService.getById(id);
		List<HairdoImg> imgs = hairdoImgService.getImgsByHairdoRecordId(id);
		model.addAttribute("t", t);
		model.addAttribute("imgs", imgs);
		return viewName("hairdodetail");
	}

	@RequestMapping(value = "hairdoedit/{id}", method = RequestMethod.GET)
	public String hairdoedit(Model model, @PathVariable String id) {
		HairdoRecord t = hairdoRecordService.getById(id);
		List<HairdoImg> imgs = hairdoImgService.getImgsByHairdoRecordId(id);
		model.addAttribute("t", t);
		model.addAttribute("imgs", imgs);
		return viewName("hairdoedit");
	}

	@RequestMapping(value = "hairdoupdate")
	public String hairdoupdate(HairdoRecord hairdoRecord,
			RedirectAttributes redirectAttr) {
		hairdoRecordService.updateByPrimaryKeySelective(hairdoRecord);
		redirectAttr.addFlashAttribute("resultMsg",
				ResultMsgFactory.success("修改成功"));
		return redirectToUrl("management/vip/buyIndex");
	}

	@RequestMapping(value = "imgadd/{id}", method = RequestMethod.GET)
	public String imgadd(Model model, @PathVariable String id) {
		// 根据员工id查询作品库
		String hairdoRecordId = id;
		model.addAttribute("hairdoRecordId", hairdoRecordId);
		return viewName("imgadd");
	}

	@RequestMapping(value = "imgsave")
	public String imgsave(HairdoImg t, RedirectAttributes redirectAttr) {
		try {
			t.setHairdoImgId(CodecUtils.getUUID());
			t.setImg(this.getphoto(t.getImg()));
			t.setCompId(this.getClientUser().getUser().getCompId());
			t.setStatedate(new Date());
			hairdoImgService.save(t);
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.success("添加成功"));
		} catch (Exception e) {
			e.printStackTrace();
			log.error("添加失败：{}", e.getMessage());
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("添加失败"));
		}
		return redirectToUrl("management/vip/hairdoedit/"
				+ t.getHairdoRecordId());
	}

	@ResponseBody
	@RequestMapping(value = "imgdelete/{id}", method = RequestMethod.POST)
	public ResultMsg imgdelete(@PathVariable String id) {
		try {
			// 删除之前先去删File中删除照片
			HairdoImg img = hairdoImgService.getById(id);
			this.delphoto(img.getImg());
			hairdoImgService.delete(id);
			return ResultMsgFactory.success("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			log.error("删除失败：{}", e.getMessage());
			return ResultMsgFactory.success("删除失败");
		}
	}

	@RequestMapping(value = "imgedit/{id}", method = RequestMethod.GET)
	public String imgedit(Model model, @PathVariable String id) {
		HairdoImg t = hairdoImgService.getById(id);
		model.addAttribute("t", t);
		return viewName("imgedit");
	}

	@RequestMapping(value = "imgupdate")
	public String imgupdate(HairdoImg t, RedirectAttributes redirectAttr) {
		try {
			t.setImg(this.getphoto(t.getImg()));
			hairdoImgService.update(t);
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.success("修改成功"));
		} catch (Exception e) {
			e.printStackTrace();
			log.error("修改失败：{}", e.getMessage());
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("修改失败"));
		}
		return redirectToUrl("management/vip/hairdoedit/"
				+ t.getHairdoRecordId());
	}

	@RequestMapping(value = "add_bak", method = RequestMethod.GET)
	public String addBak(Model model) {
		return viewName("add_bak");
	}
	@RequestMapping("/payFlowIndex")
	public String payFlowIndex() {
		return "management/vip/payFlowIndex";
	}
	@ResponseBody
	@RequestMapping("payFlowpage")
	public Page<PayFlow> payFlowpage(PayFlow payFlow,Querier querier) {
		//默认时间本月，默认方式
		if(payFlow.getPayWay()==null){
			payFlow.setPayWay("XJ");
		}
		if(payFlow.getBegDate()==null&&payFlow.getEndDate()==null){
			payFlow.setBegDate(DateUtil.getStartTime(DateUtil.getMonthFirstDate(new Date(), 0)));
			payFlow.setEndDate(DateUtil.getEndTime(DateUtil.getMonthLastDate(new Date(), 0)));
		}else{
			payFlow.setBegDate(DateUtil.getStartTime(payFlow.getBegDate()));
			payFlow.setEndDate(DateUtil.getEndTime(payFlow.getEndDate()));
		}
		Page<PayFlow> page = payFlowService.queryPayFlowpage(querier,
				payFlow);
		return page;
	}
	@ResponseBody
	@RequestMapping("getpayFlowAmount")
	public ResultMsg getpayFlowAmount(PayFlow payFlow) {
		//默认时间本月，默认方式
		if(payFlow.getPayWay()==null){
			payFlow.setPayWay("XJ");
		}
		if(payFlow.getBegDate()==null&&payFlow.getEndDate()==null){
			payFlow.setBegDate(DateUtil.getStartTime(DateUtil.getMonthFirstDate(new Date(), 0)));
			payFlow.setEndDate(DateUtil.getEndTime(DateUtil.getMonthLastDate(new Date(), 0)));
		}else{
			payFlow.setBegDate(DateUtil.getStartTime(payFlow.getBegDate()));
			payFlow.setEndDate(DateUtil.getEndTime(payFlow.getEndDate()));
		}
		String count = payFlowService.getpayFlowAmount(payFlow);
		System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==== "+count);
		return ResultMsgFactory.success(count.toString());
	}
	
	@RequestMapping("/infoIndex")
	public String infoIndex() {
		return "management/vip/infoIndex";
	}
	@ResponseBody
	@RequestMapping("codeInfopage")
	public Page<IdentifyCode> codeInfopage(IdentifyCode identifyCode,Querier querier) {
		Page<IdentifyCode> page = identifyCodeService.queryByPage(querier, identifyCode);
		return page;
	}
}
